package com.ywb.edugui.dao.impl;

import com.alibaba.excel.EasyExcel;
import com.aliyun.oss.common.utils.StringUtils;
import com.ywb.edugui.dao.TeacherDao;
import com.ywb.edugui.entity.Teacher;
import com.ywb.edugui.entity.vo.TeacherQuery;
import com.ywb.edugui.utils.MySnowflakeIdWorker;
import com.ywb.edugui.utils.SnowflakeIdWorker;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * DaoImpl 2.0 创建BaseDaoImpl提取出所有相同的步骤
 * 使用CommonDButils实现结果集的封装,只需要传入sql和?相对应的数据即可返回数据
 */
public class TeacherDaoImpl extends BaseDaoImpl<Teacher> implements TeacherDao{

    @Override
    public List<Teacher> list() {
        String sql = "select * from teacher where is_deleted = 0 order by name";
//        String sql = "select * from teacher where is_deleted = 0";
        return list(sql);
    }

    /**
     * 多条件查询，将会触发Teacher表中的联合索引
     * @param query
     * @return
     */
    @Override
    public List<Teacher> list(TeacherQuery query) {
        if(query.getTeacherIntro() == null && query.getTeacherLevel() == 0 && query.getTeacherName() == null && query.getTeacherOrderCondition() == null)
            return this.list();

        StringBuilder sql = new StringBuilder("select * from teacher where is_deleted = 0");
        List<Object> parameters = new ArrayList<>();

        if(query.getTeacherLevel() > 0 ) {
            sql.append(" and level = ?");
            parameters.add(query.getTeacherLevel());
        }

        if(!StringUtils.isNullOrEmpty(query.getTeacherName())) {
            sql.append(" and name like ?");
            parameters.add("%" + query.getTeacherName() + "%");
        }

        if(!StringUtils.isNullOrEmpty(query.getTeacherIntro())){
            sql.append(" and intro like ?");
            parameters.add("%" + query.getTeacherIntro() + "%");
        }


        if(!StringUtils.isNullOrEmpty(query.getTeacherOrderCondition())){
            sql.append(" order by " + query.getTeacherOrderCondition() + " ");
//            parameters.add(query.getTeacherOrderCondition());
            if(query.getTeacherOrder() != null && query.getTeacherOrder().equals("desc")){
                sql.append(" desc");
            }
        }

        return list(sql.toString(), parameters.toArray());
    }


    @Override
    public Teacher getOne(String id) {
        String sql = "select * from teacher where is_deleted = 0 and id = ?";
        return getOne(sql, id);
    }

    @Override
    public int add(Teacher teacher) {
        if(StringUtils.isNullOrEmpty(teacher.getId())){
            teacher.setId(MySnowflakeIdWorker.nextId());
        }
        String sql = "insert into teacher(id, name, intro, level, avatar, is_deleted) values(?,?,?,?,?,?)";
        return update(sql, teacher.getId(), teacher.getName(), teacher.getIntro(), teacher.getLevel(),
                teacher.getAvatar(), teacher.getIs_deleted());
    }

    @Override
    public int delete(Teacher teacher) {
        String sql = "update teacher set is_deleted = 1 where id = ?";
        return update(sql, teacher.getId());
    }

    @Override
    public int delete(String id) {
        String sql = "update teacher set is_deleted = 1 where id = ?";
        return update(sql, id);
    }

    @Override
    public int update(Teacher teacher) {
        String sql = "update teacher set name = ?, intro = ?, level = ?, avatar = ?, is_deleted = ? where id = ?";
        return update(sql, teacher.getName(), teacher.getIntro(), teacher.getLevel(),
                teacher.getAvatar(), teacher.getIs_deleted(), teacher.getId());
    }

}
